import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { ConfigService } from '@nestjs/config';
import { Injectable } from '@nestjs/common';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly config: ConfigService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), // 提供从 Request 中提取 JWT 的方法。我们将使用标准方法在 API 请求的授权标头中提供不记名令牌。
      ignoreExpiration: false, // 设置false, 确保 JWT 未过期的责任委托给 Passport 模块
      secretOrKey: config.get('jwt.secret'), // 加密秘钥
    });
  }

  async validate(payload: any) {
    return { userId: payload.sub, username: payload.username };
  }
}
